SIZE = 8

board = [[0 for i in range(SIZE)]for j in range(SIZE)]

slotInfo = [
    [-2, -1], [-2, 1], 
    [-1, -2], [-1, 2], 
    [1, -2], [1, 2], 
    [2, -1], [2, 1]
]


def TryStep(x, y, slot):
    global board
    info = slotInfo[slot]
    nx = x + info[0]
    ny = y + info[1]

    if nx >=0 and nx < SIZE and ny >= 0 and ny < SIZE and board[nx][ny] == 0:
        return True, nx, ny
    
    return False, x, y


# def TryStep(x, y, slot):
#     global board
    
#     if slot==0 and x-2>=0 and y-1>=0 and board[x-2][y-1]==0:
#         return True, x-2, y-1
#     elif slot==1 and x-2>=0 and y+1<SIZE and board[x-2][y+1]==0:
#         return True, x-2, y+1
#     elif slot==2 and x-1>=0 and y-2>=0 and board[x-1][y-2]==0:
#         return True, x-1, y-2
#     elif slot==3 and x-1>=0 and y+2<SIZE and board[x-1][y+2]==0:
#         return True, x-1, y+2
#     elif slot==4 and x+1<SIZE and y-2>=0 and board[x+1][y-2]==0:
#         return True, x+1, y-2
#     elif slot==5 and x+1<SIZE and y+2<SIZE and board[x+1][y+2]==0:
#         return True, x+1, y+2
#     elif slot==6 and x+2<SIZE and y-1>=0 and board[x+2][y-1]==0:
#         return True, x+2, y-1
#     elif slot==7 and x+2<SIZE and y+1<SIZE and board[x+2][y+1]==0:
#         return True, x+2, y+1
#     else:
#         return False, x, y

def Output():
    global board
    for i in board:
        print(i)

def Travel(x, y, step):
    global board
    board[x][y] = step
    if step == SIZE * SIZE:
        Output()
        return True

    slot = 0
    while slot < SIZE:
        result, nx, ny = TryStep(x, y, slot)
        if result == True:
            statues = Travel(nx, ny, step + 1)
            if statues==True:
                return True
        slot += 1
    
    board[x][y] = 0

    return False

print(Travel(3, 3, 1))